import json

import requests

url = 'https://api.coze.cn/open_api/v2/chat'
bot_id_board = '7382122334401921078'
bot_id_sd = '7387296821271543819'
bot_id_translate = '7387303687678591010'
api_key = 'pat_eOiRR2lxYIUQ8LjaI4E1R01M0jLy8ufBtii1OaLtOo8Zach0eLUMBjtDr31QbKkt'


def send_message(bot_id, userid, conversation_id,  message):
    headers = {
        "Content-Type": "application/json",
        'Authorization': f'Bearer {api_key}',
        'Connection': 'keep-alive',
        'Accept': '*/*',
    }
    body = {
        'bot_id': bot_id,
        'conversation_id': conversation_id,
        'user': userid,
        'query': message,
        'stream': False,
        # 'custom_variables': {'command_type': command_type,
        #                      'book_content': book_content
        #                      }

    }
    response = requests.post(url, headers=headers, json=body)
    data = response.json()
    if data['msg'] == 'success':
        conversation_id = data['conversation_id']
        messages = data['messages']
        if 'sd_prompts_generate' in messages[0]['content']:
            print(messages[2]['content'])
            return (conversation_id, json.loads(messages[2]['content']))
        elif bot_id == bot_id_translate:
            print(messages[0]['content'])
            return (conversation_id, messages[0]['content'])
    return (None, None)


